package com.tjbaobao.videoplayol.ui;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

/**
 * 按钮进度条(实例化对象后，只需要调用setProgress()函数就能更新进度,调用finish（）结束)
 * @author TJbaobao
 *
 */
public class ButtonProgressView extends View {

	private Context context ;
	private float progress=0;
	private int backgroundColor= Color.parseColor("#eb3953") ;//默认背景颜色
	private int progressColor = Color.parseColor("#eb3953");//默认进度条颜色
	private int progressBGColor = Color.parseColor("#c9c9c9");//默认进度条背景颜色
	private int textColor = Color.parseColor("#ffffff");//默认进度条背景颜色
	private String finishTipStr = "上传完成";//完成任务时的提示
	private boolean bStart ;
	private String text = "";
	private int textSize = 30;//字体大小
	private int viewWidth,viewHeight;
	private boolean bFinish = false;
	private String speed = "0";
	/**
	 * 自定义组件构造方法(如果想放在xml里就至少需要以下三个字符串)
	 * @param context
	 */
	public ButtonProgressView(Context context) {
		super(context);
		this.context = context ;
	}
	public ButtonProgressView(Context context, AttributeSet attrs)
	{
		super(context ,attrs);
		this.context = context ;
	}
	public ButtonProgressView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context ,attrs,defStyle);
		this.context = context ;
	}
	@Override
	protected void onDraw(Canvas canvas) {
		Paint paint = new Paint();
		paint.setAntiAlias(true);// 消除锯齿效果
		if(bStart)
		{
			//启动进度条
			canvas.drawColor(progressBGColor);
			paint.setColor(progressColor);
			paint.setStyle(Paint.Style.FILL);
			float width = progress*viewWidth/100;
	        canvas.drawRect(0, 0, width, viewHeight, paint);
	        
	        //绘画文字
	        if(bFinish)
	        {
	        	bStart = false;
	        	textColor = Color.WHITE;
	        	drawTextCenter(canvas,finishTipStr);
	        }
	        else
	        {
	        	if(progress>45)
				{
	        		textColor = Color.WHITE;
				}
				else
				{
					textColor = Color.parseColor("#363636");
				}
	        	if(speed.equals(.0))
	        	{
	        		speed = "0";
	        	}
	        	String uishow = "";
	        	if(progress>=100)
		        {
		        	uishow = "上传即将完成";
		        }
	        	else
	        	{
	        		uishow = progress+"%"+" ("+speed+"kb/s)";
	        	}
	        	drawTextCenter(canvas,uishow);
	        }
		}
		else
		{
			//未启动进度条
			canvas.drawColor(backgroundColor);
			paint.setColor(textColor);
			drawTextCenter(canvas,text);
		}
	}
	/**
	 * 设置进度条进度值 %
	 * @param progress 进度值百分比
	 */
	public void setProgress(float progress)
	{
		if(!bStart)
		{
			bStart = true;
		}
		this.progress = progress ;
		postInvalidateDelayed(1);  
	}
	/**
	 * 出错重置状态以及设置错误提示
	 * @param text
	 */
	public void setError(String text)
	{
		bStart = false;
		this.text = text;
		postInvalidateDelayed(1);
	}
	public void initView()
	{
		bStart = false;
		textColor = Color.WHITE;
		postInvalidateDelayed(1);
	}
	/**
	 * 完成任务(调用此函数后才能100%提示下载完成)
	 */
	public void finish()
	{
		bFinish = true;
		postInvalidateDelayed(1);
	}
	
	//------------------下方的内容可以不用管-------------------------------------------------
	/**
	 * 使文字完全居中
	 * @param canvas
	 * @param text
	 */
	private void drawTextCenter(Canvas canvas, String text)
	{
		Paint mPaint = new Paint();
		mPaint.setAntiAlias(true);// 消除锯齿效果
		mPaint.setStrokeWidth(3);
		mPaint.setTextSize(textSize);
		mPaint.setColor(textColor);
		mPaint.setTextAlign(Align.LEFT);
		Rect bounds = new Rect();
		mPaint.getTextBounds(text, 0, text.length(), bounds);
		FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
		int baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
		canvas.drawText(text, getMeasuredWidth() / 2 - bounds.width() / 2, baseline, mPaint);
	}
	private int getColorForRes(int resID)
	{
		return context.getResources().getColor(resID);
	}
	/**
	 * 计算xml布局里的view大小
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		this.viewWidth = measureWidth(widthMeasureSpec);  
        this.viewHeight = measureHeight(heightMeasureSpec);  
        setMeasuredDimension(viewWidth, viewHeight);
	}
	/**
	 * 计算组件宽度
	 */
	private int measureWidth(int measureSpec) {
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);
		int result = 500;
		if (specMode == MeasureSpec.AT_MOST) {
			result = specSize;
		}
		else if (specMode == MeasureSpec.EXACTLY) {

			result = specSize;
		}
		return result;
	}

	/**
	 * 计算组件高度
	 */
	private int measureHeight(int measureSpec) {

		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);
		int result = 500;
		if (specMode == MeasureSpec.AT_MOST) {
			result = specSize;
		} else if (specMode == MeasureSpec.EXACTLY) {

			result = specSize;
		}
		return result;
	}
	
	public void setMBackgroundColor(int backgroundColor) {
		this.backgroundColor = backgroundColor;
		//刷新
		postInvalidateDelayed(1); 
	}
	public int getProgressColor() {
		return progressColor;
	}
	public void setProgressColor(int progressColor) {
		this.progressColor = progressColor;
	}
	public int getProgressBGColor() {
		return progressBGColor;
	}
	public void setProgressBGColor(int progressBGColor) {
		this.progressBGColor = progressBGColor;
	}
	public int getTextColor() {
		return textColor;
	}
	public void setTextColor(int textColor) {
		this.textColor = textColor;
	}
	public void setText(String text) {
		this.text = text;
		postInvalidate();
	}
	public int getTextSize() {
		return textSize;
	}
	public void setTextSize(int textSize) {
		this.textSize = textSize;
	}
	public String getSpeed() {
		return speed;
	}
	public void setSpeed(String speed) {
		this.speed = speed;
		postInvalidateDelayed(1); 
	}
	
	
}
